layui.use(['form', 'table', 'fsoft'], function () {
	var $ = layui.$
		, form = layui.form
		, table = layui.table
		, fsoft = layui.fsoft;
	var tableId = "table";
	var searchFormId = "fsoftForm";
	//字典列表刷新
	table.render({
		elem: '#' + tableId
		, method: 'post'
		, toolbar: '#sysdict-tbs-toolbar'
		, url: layui.cache['contentPath'] + '/sys-dict/findList'
		, cols: [[
			{ type: 'checkbox', fixed: 'left', width: 50 },
			{ field: 'code', title: '编码', width: 150, sort: true },
			{ field: 'name', title: '名称', minWidth: 200, width: 300, sort: true },
			{
				field: 'dictType', title: '类型', width: 80, align: 'center'
				, templet: function (item) {
					if (item.dictType == 1) {
						return '<span class="layui-badge layui-bg-blue">列表</span>'
					} else if (item.dictType == 2) {
						return '<span class="layui-badge">树形</span>'
					} else {
						return '<span class="layui-badge">其他</span>'
					}
				}
			},
			{ field: 'status', title: '状态', width: 110, align: 'center', templet: '#switchStatus' },
			{ field: 'sortNo', title: '排序', width: 80, align: 'center' },
			{ fixed: 'right', width: 190, align: 'center', title: '操作', toolbar: '#sysdict-tbs', fixed: 'right' }
		]],
		page: true,
		limit: 20,
		limits: [20, 30, 50, 100],
		height: 'full-96'
	});
	//搜索监听
	form.on('submit(sreach)', function (data) {
		table.reload('table', { where: data.field });
		return false;
	});
	//行操作监听 
	table.on('tool(tbs)', function (obj) {
		var data = obj.data;
		if (obj.event === 'setting') {
			layer.open({
				id: "sysdict-settings-form"
				, type: 2
				, title: data.name + '-字典设置'
				, area: ['80%', '85%']
				, shadeClose: false
				, content: layui.cache['contentPath'] + '/sys-dict/items/' + data.id
			});
		} else if (obj.event === 'edit') {
			layer.open({
				id: "sysdict-modify-form"
				, type: 2
				, title: data.name + '-字典编辑'
				, area: ['500px', '350px']
				, shadeClose: false
				, content: layui.cache['contentPath'] + '/sys-dict/edit/' + data.id
				, end: function () {
					frmActiveAction.doSearch();
				}
			});
		} else if (obj.event === 'del') {
			layer.confirm("确认要删除当前记录？", function (index) {
				fsoft.delete(layui.cache['contentPath'] + '/sys-dict/remove', [data.id], function (rs) {
					if (rs.code == 0) {
						layer.msg("删除成功", { icon: 1, time: 2000 });
						table.reload('table', { where: data.field });
					} else {
						layer.alert(rs.msg);
					}
				});
				layer.close(index);
			});
		}
	});
	/* 工具条（table顶部）监听 */
	table.on('toolbar(tbs)', function (obj) {
		var checkStatus = table.checkStatus(obj.config.id);
		switch (obj.event) {
			case 'add':
				frmActiveAction.add();
				break;
			case 'removeBatch':
				frmActiveAction.removeBatch(checkStatus.data);
				break;
			case 'enable':
				frmActiveAction.enable(checkStatus.data);
				break;
			case 'disable':
				frmActiveAction.disable(checkStatus.data);
				break;
		};
	});
	//状态
	form.on('switch(status)', function (data) {
		var msg = "启用成功";
		var submitType = "enable";
		if (data.elem.checked == false) {
			msg = "禁用成功";
			submitType = "disable";
		}
		var ids = new Array();
		ids.push(this.value);
		fsoft.put(layui.cache['contentPath'] + '/sys-dict/' + submitType, ids, function (rs) {
			if (rs.code == 0) {
				layer.msg(msg, { icon: 1, time: 2000 });
				table.reload('table', { where: data.field });
			} else {
				layer.msg(rs.msg, { icon: 2, time: 2000 });
			}
		});
	});

	var frmActiveAction = {
		doSearch: function (params) {
			if (params == undefined || params == null)
				params = $.extend({}, form.val(searchFormId));
			else
				$.extend(params, form.val(searchFormId));
			table.reload(tableId, { where: params });
		}
		, removeBatch: function (data) { //批量删除
			if (data == undefined || data.length == 0) {
				layer.msg("请至少选择一条记录", { icon: 7, time: 2000 });
				return;
			}
			var ids = new Array();
			$.each(data, function (idx, item) {
				ids.push(item.id);
			});
			layer.confirm("确认要删除已选中的（" + ids.length + "）条记录？", function (index) {
				fsoft.delete(layui.cache['contentPath'] + '/sys-dict/remove', ids, function (rs) {
					if (rs.code == 0) {
						fsoft.successMsg("删除成功");
						frmActiveAction.doSearch();
					} else {
						fsoft.errorMsg(rs.msg);
					}
					layer.close(index);
				});
			});
		},
		add: function () {
			layer.open({
				type: 2, title: "新增字典"
				, area: ['500px', '350px']
				, content: layui.cache['contentPath'] + '/sys-dict/add'
			});
		},
		enable: function (data) {/*启用*/
			if (data == undefined || data.length == 0) {
				fsoft.warnMsg("请至少选择一条记录");
				return false;
			}
			var ids = new Array();
			$.each(data, function (idx, item) {
				if (item["status"] != undefined && item["status"] == 0)
					ids.push(item.id);
			});
			if (ids.length == 0) {
				fsoft.warnMsg("请至少选择1条禁用记录");
				return false;
			}
			var confirmMsg = "确认要启用已选中的（" + ids.length + "）条字典？";
			if (data.length != ids.length) {
				confirmMsg = "已选中" + data.length + "条字典，其中只有" + ids.length + "条记录需要启用，是否继续？";
			}
			fsoft.confirm('询问', confirmMsg, function (index) {
				fsoft.put(layui.cache['contentPath'] + '/sys-dict/enable', ids, function (rs) {
					if (rs.code == 0) {
						fsoft.successMsg("启用成功");
						frmActiveAction.doSearch();
					} else {
						fsoft.errorMsg(rs.msg);
					}
					layer.close(index);
				});
			});
		},
		disable: function (data) { /*禁用*/
			if (data == undefined || data.length == 0) {
				fsoft.warnMsg("请至少选择一条记录");
				return;
			}
			var ids = new Array();
			$.each(data, function (idx, item) {
				if (item["status"] != undefined && item["status"] == 1)
					ids.push(item.id);
			});
			if (ids.length == 0) {
				fsoft.warnMsg("请至少选择1条启用记录");
				return false;
			}
			var confirmMsg = "确认要禁用已选中的（" + ids.length + "）条字典？";
			if (data.length != ids.length) {
				confirmMsg = "已选中" + data.length + "条字典，其中只有" + ids.length + "条记录需要禁用，是否继续？";
			}
			fsoft.confirm('询问', confirmMsg, function (index) {
				fsoft.put(layui.cache['contentPath'] + '/sys-dict/disable', ids, function (rs) {
					if (rs.code == 0) {
						fsoft.successMsg("禁用成功");
						frmActiveAction.doSearch();
					} else {
						layer.alert(rs.msg);
					}
					layer.close(index);
				});
			});
		}
	};
});